//
//  AppDelegate.swift
//  weibo
//
//  Created by nick on 15/9/22.
//  Copyright © 2015年 cn.nick. All rights reserved.
//

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?


    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        setUpAppearance()
        
        
        window = UIWindow(frame: UIScreen.mainScreen().bounds)
//        let vc = TabBarViewController()
//        let vc = NewFeatureViewController()
//        let vc = WellcomeBackViewController()
//        vc.view.backgroundColor = UIColor.orangeColor()
        window?.rootViewController = defaultRootViewController()
        window?.makeKeyAndVisible()
        
        // 添加通知
        NSNotificationCenter.defaultCenter().addObserver(self, selector: "changeRootVcByNotification:", name: WBChangeVCNotificationKey, object: nil)
        return true
    }
    // 移除通知
    deinit {
        NSNotificationCenter.defaultCenter().removeObserver(self)
    }
    
    // 监听通知方法
    @objc private func changeRootVcByNotification(notification: NSNotification) {
        let vc = (notification.object != nil) ? WellcomeBackViewController() : HomeTableViewController()
        window?.rootViewController = vc
    }
    
    // 切换跟视图
    private func defaultRootViewController() -> UIViewController {
        if UserAccountViewModel.userAccountViewModel.userLogon {
            return isNewVerson() ? NewFeatureViewController() : WellcomeBackViewController()
        }
        return TabBarViewController()
    }
    private func isNewVerson() -> Bool {
        
        // 获得当前版本
        let currentVerson = NSBundle.mainBundle().infoDictionary!["CFBundleShortVersionString"] as! String
        let verson = Double(currentVerson)!
        
        // 取出之前版本
        let senBoxKeyString = "senBoxKeyString"
        let senBoxKey = NSUserDefaults.standardUserDefaults().doubleForKey(senBoxKeyString)
        
        // 保存当前版本
        NSUserDefaults.standardUserDefaults().setDouble(verson, forKey: senBoxKeyString)
        
        return verson > senBoxKey
        
        
    }
    
    func setUpAppearance() {
        UINavigationBar.appearance().tintColor = WBAppearanceColor
        UITabBar.appearance().tintColor = WBAppearanceColor
    }


}

